<template>
 <div>
   <el-container class="container">
     <el-header>
       <span class="title">校园二手交易市场</span>
       <el-button class="returnBtn" type="primary" @click="backToLogin">返回登录</el-button>
     </el-header>
     <el-main>
       <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
         <el-form-item label="用户名" prop="username">
           <el-input v-model="ruleForm.username"></el-input>
         </el-form-item>
         <el-form-item label="密码" prop="password">
           <el-input v-model="ruleForm.password" type="password"></el-input>
         </el-form-item>
         <el-form-item label="确认密码" prop="checkPassword">
           <el-input v-model="ruleForm.checkPassword" type="password"></el-input>
         </el-form-item>
         <el-form-item label="性别" prop="sex">
           <el-radio-group v-model="ruleForm.sex">
             <el-radio label="男"></el-radio>
             <el-radio label="女"></el-radio>
           </el-radio-group>
         </el-form-item>
         <el-form-item label="电话" prop="telephone">
           <el-input v-model="ruleForm.telephone"></el-input>
         </el-form-item>
         <el-form-item label="邮箱" prop="email">
           <el-input v-model="ruleForm.email"></el-input>
         </el-form-item>
         <el-form-item label="住址" prop="city">
           <el-input v-model="ruleForm.city"></el-input>
         </el-form-item>
         <el-form-item label="银行账户" prop="bankaccount">
           <el-input v-model="ruleForm.bankaccount"></el-input>
         </el-form-item>
         <el-form-item label="验证码" prop="verify">
           <el-input v-model="ruleForm.verify" class="verifyInput"></el-input>
           <img :src="url" alt="验证码" class="verifyImg" @click="getVerify">
         </el-form-item>
         <el-form-item>
           <el-button type="primary" @click="submitForm('ruleForm')">立即注册</el-button>
           <el-button @click="resetForm('ruleForm')">重置</el-button>
         </el-form-item>
       </el-form>
     </el-main>
   </el-container>
 </div>
</template>

<script>
export default {
  name: "register",
  data() {
    var checkedPassword = (rule, value, callback) => {
      if(value === ''){
        callback(new Error("请再次输入密码"))
      } else if(value !== this.ruleForm.password){
        callback(new Error("两次输入密码不一致"))
      } else {
        callback()
      }
    }
    var checkVerify = (rule, value, callback) => {
      if(value === ''){
        callback(new Error('请输入验证码'))
      } else if(value !== this.verifyCode) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: '',
        password: '',
        checkPassword: '',
        sex: '',
        telephone: '',
        email: '',
        city: '',
        bankaccount: '',
        verify: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        checkPassword: [
          { validator: checkedPassword, trigger: 'blur' },
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' },
        ],
        telephone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 8, max: 20, message: '长度在 8 到 20 个字符', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '请输入所在地', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        bankaccount: [
          { required: true, message: '请输入银行账户', trigger: 'blur' },
          { min: 16, max: 16, message: '长度为16位', trigger: 'blur' }
        ],
        verify: [
          { validator: checkVerify, trigger: 'blur' }
        ]
      },
      verifyCode: '',
      url: ''
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/register', this.ruleForm)
              .then(res => {
                this.$message.success(res.data.msg, {duration: 3*1000})
                this.resetForm('ruleForm')
              })
          this.addBank()
        } else {
          this.getVerify()
          return false;
        }
      });
    },
    addBank(){
      this.$axios.post("/addBank", {
        bankAccount: this.ruleForm.bankaccount
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    backToLogin(){
      this.$router.back()
    },
    getVerify(){
      const randomNum = parseInt(Math.random()*50)
      const url = `http://localhost:8081/verify?num=${randomNum}`
      this.url = this.timeStamp(url)
      this.$axios.get(`/code?num=${randomNum}`)
          .then(res => {
            this.verifyCode = res.data.data
          })
    },
    timeStamp(url){
      var getTimeStamp = new Date().getTime()
      return url + '&timestamp' + getTimeStamp
    },
  },
  created() {
    this.getVerify()
  }
}
</script>

<style scoped>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.container{
  max-width: 960px;
  margin: 0 auto;
}
.title{
  font-size: 50px;
}
.demo-ruleForm{
  max-width: 400px;
  margin: 0 auto;
}
.returnBtn{
  position: relative;
  top: -15px;
  left: 100px;
}
.verifyInput{
  position: absolute;
  left: 0;
  width: 150px;
}
.verifyImg{
  position: absolute;
  margin-left: 30px;
  width: 90px;
}
</style>